package com.jiuyin.dianjing.ui.activity.community;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.GetTagListResp;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;

public class TagActivity extends BaseActivity {

    public static final String KEY_GAME_TAG = "game";
    public static final String KEY_DESIRE_TAG = "desire";

    @BindView(R.id.flow_desire)
    TagFlowLayout flowDesire;
    @BindView(R.id.flow_game)
    TagFlowLayout flowGame;
    @BindView(R.id.bt_ok)
    Button btOK;
    String desire, game;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_tag;
    }

    @Override
    protected void initData() {
        game = getIntent().getStringExtra(KEY_GAME_TAG);
        desire = getIntent().getStringExtra(KEY_DESIRE_TAG);
        //server取数据
        HashMap<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, HelperApplication.getToken());
        JSONObject json = new JSONObject(map);
        ServerApi.post(ApiEnum.APP_GET_TAG_LIST.getUrl(), json, null, null, d -> addDisposable(d), new ApiResultCallback() {

            private GetTagListResp getTagListResp;

            @Override
            public void onSuccess(JsonObject responseData) {
                getTagListResp = new Gson().fromJson(responseData, GetTagListResp.class);
                setData();
            }

            @Override
            public void onFail(String msg) {

            }

            @Override
            public void onError(String msg) {

            }

            void setData() {
                final LayoutInflater mInflater = LayoutInflater.from(TagActivity.this);
                TagAdapter<String> desireAdapter = new TagAdapter<String>(getTagListResp.genre_tag_List) {

                    @Override
                    public View getView(FlowLayout parent, int position, String s) {
                        TextView tv = (TextView) mInflater.inflate(R.layout.tv_tag,
                                flowDesire, false);
                        tv.setText(s);
                        return tv;
                    }
                };
                TagAdapter<String> gameAdapter = new TagAdapter<String>(getTagListResp.game_tag_List) {

                    @Override
                    public View getView(FlowLayout parent, int position, String s) {
                        TextView tv = (TextView) mInflater.inflate(R.layout.tv_tag,
                                flowDesire, false);
                        tv.setText(s);
                        return tv;
                    }
                };
                flowDesire.setAdapter(desireAdapter);
                flowGame.setAdapter(gameAdapter);
                flowDesire.setOnSelectListener(selectPosSet -> {
                    if (selectPosSet.isEmpty()) {
                        desire = "";
                    } else {
                        int position = selectPosSet.iterator().next();
                        desire = getTagListResp.genre_tag_List[position];
                    }
                });
                flowGame.setOnSelectListener(selectPosSet -> {
                    if (selectPosSet.isEmpty()) {
                        game = "";
                    } else {
                        int position = selectPosSet.iterator().next();
                        game = getTagListResp.game_tag_List[position];
                    }
                });
                //回显 选中标签
                if (!TextUtils.isEmpty(desire)) {
                    int position = -1;
                    for (int i = 0; i < getTagListResp.genre_tag_List.length; i++) {
                        if (getTagListResp.genre_tag_List[i].equals(desire)) {
                            position = i;
                            break;
                        }
                    }
                    if (position > -1) {
                        desireAdapter.setSelectedList(position);
                    }
                }
                if (!TextUtils.isEmpty(game)) {
                    int position = -1;
                    for (int i = 0; i < getTagListResp.game_tag_List.length; i++) {
                        if (getTagListResp.game_tag_List[i].equals(game)) {
                            position = i;
                            break;
                        }
                    }
                    if (position > -1) {
                        gameAdapter.setSelectedList(position);
                    }
                }
            }
        });
    }


    @Override
    protected void initView() {
        setTitle("添加标签");
        btOK.setOnClickListener(v -> {
            Intent data = new Intent();
            data.putExtra(KEY_DESIRE_TAG, desire);
            data.putExtra(KEY_GAME_TAG, game);
            setResult(RESULT_OK, data);
            finish();
        });
    }
}
